# -*-coding: utf-8 -*-

import scrapy
from scrapy.loader import ItemLoader
from chinahr.items import JobInfoItem, ComInfoItem
from scrapyluke.processors import *


class ZhaopinCrawlSpider(scrapy.Spider):

    name = 'zhaopinxiaoyuan'
    allowed_domains = ['xiaoyuan.zhaopin.com']
    start_urls = ['http://xiaoyuan.zhaopin.com/FindFullTime/FullTimeSearch/SearchResult/?SearchModel=0&&ref=jobsearch&&OB=0&&ST=1&&PageSize=1000']

    # 数据解析方法
    def parse(self, response):
        Maxpage_Id = ''.join(response.xpath('//ul[@id="page"]/li[last()-1]/span/text()').extract())
        for i in range(1, int(Maxpage_Id)+1):
            yield scrapy.Request('%s&&PageNumber=%d' % (response.url, i), callback=self.parse_list)

    def parse_list(self, response):
        job_urls = response.xpath('//ul[@class="domList1"]/li/div[@class="conList"]/div[@class="clearfix"]/h4/a[not(contains(@socialhref, "http://jobs.zhaopin.com"))]/@href').extract()
        com_urls = response.xpath('//ul[@class="domList1"]/li/div[@class="conList"]/div[@class="clearfix"]/h4/a[not(contains(@socialhref, "http://jobs.zhaopin.com"))]/../../h5/a/@href').extract()
        for url in job_urls:
            yield scrapy.Request("http://xiaoyuan.zhaopin.com%s" % url, callback=self.parse_job_info)
        for url in com_urls:
            yield scrapy.Request("http://xiaoyuan.zhaopin.com%s" % url, callback=self.parse_com_info)

    def parse_job_info(self, response):
        loaderJob = ItemLoader(item=JobInfoItem(), response=response)
        loaderJob.add_value('url', value=response.url)
        loaderJob.add_xpath('job_name', u'//div[@class="cWrap"]/div[@class="cMain"]/div[2]//h1[@id="JobName"]/text()', TakeFirstL())
        loaderJob.add_xpath('job_company', u'//div[@class="cRight r"]//div[@class="cCompanyInfoCon r"]/a[@class="cCompanyName"]/text()', TakeFirstL())
        loaderJob.add_xpath('job_company', u'//div[@class="cWrap"]/div[@class="cMain"]/div[2]//h1[@id="JobName"]/span/span/text()', TakeFirstL())
        loaderJob.add_xpath('job_benefits', u'//div[@class="cLabelList clearfix"]/span/text()', JoinL('|'))
        loaderJob.add_xpath('job_salary', u'//ul[@class="cJobDetailInforBotWrap clearfix c3"]', re=u'资：</li>[\w\W]*?marb">(.*?)</li>?')
        loaderJob.add_xpath('job_location', u'//div[@class="cMain"]/div[@class="cMainBg"]/div[@class="cJobDetail_container clearfix"]/div/div/ul/li[@id="currentJobCity"]/text()', TakeFirstL())
        loaderJob.add_xpath('job_update', u'//ul[@class="cJobDetailInforBotWrap clearfix c3"]', TakeFirstL(), re=u'发布时间：</li>[\w\W]*?marb">(.*?)</li>?')
        loaderJob.add_xpath('job_recruNums', u'//ul[@class="cJobDetailInforBotWrap clearfix c3"]', re=u'(?=招聘人数：</li>)[\s\S]*?marb">(.*?)</li>?')
        loaderJob.add_xpath('job_category', u'//ul[@class="cJobDetailInforBotWrap clearfix c3"]', re=u'职位类别：</li>[\s\S]*?marb">(.*?)<?/li>')
        loaderJob.add_xpath('job_desc', u'//div[@class="j_cJob_Detail"]/div//p[@class="mt20"]/text()')
        return loaderJob.load_item()

    def parse_com_info(self, response):
        loaderCom = ItemLoader(item=ComInfoItem(), response=response)
        loaderCom.add_value('url', value=response.url)
        loaderCom.add_xpath('com_name', u'//span[@class="cCompanyTop_name l"]/text()')
        loaderCom.add_xpath('com_industry', u'//div[@class="cCompanyGuild"]/p/span/text()', re=u'公司行业：(.*)')
        loaderCom.add_xpath('com_size', u'//div[@class="cCompanyGuild"]/p/span/text()', re=u'公司规模：(.*)')
        loaderCom.add_xpath('com_nature', u'//div[@class="cCompanyGuild"]/p/span/text()', re=u'公司类型：(.*)')
        loaderCom.add_xpath('com_benefits', u'//div/h4[contains(text(),"公司标签")]/../div/span/text()', JoinL('|'))
        loaderCom.add_xpath('com_address', u'//div/h4[contains(text(),"公司地址")]/../div/p/text()', ReplaceBlank(''), TakeFirstL())
        loaderCom.add_xpath('com_link', u'//div/h4[contains(text(),"公司地址")]/../div/p[contains(text(), "网址")]/a/@href')
        return loaderCom.load_item()



